package com.ecoolex.ying.space.api.graphql;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.alibaba.fastjson.JSONArray;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.EstateProjectActivity;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoods;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModel;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelItems;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelItemsGoods;
import com.ecoolex.ying.space.entity.PickGoodsWithBLOBs;
import com.ecoolex.ying.space.ws.EstateProjectActivityService;
import com.ecoolex.ying.space.ws.PickGoodsService;

@GraphQL2Module(type = GraphQL2Module.Type.User, description = "地产项目活动户型方案查询")
public class EstateProjectActivityGraphql {
	
	@Autowired
    private StorageService storageService;
	
	@Reference
	private EstateProjectActivityService service;
	
	@Reference
	private PickGoodsService pickGoodsService;
	
	@TypeField(type = EstateProjectActivity.class,field = "houseModelList",description = "户型方案列表")
	public Pagination<EstateProjectActivityHouseModel> houseModelList(@Source EstateProjectActivity source,@NonNull Page page){
		return service.modelList(source.getProjectId(),page);
	}
	
	@TypeField(type = EstateProjectActivityGoods.class,field = "pickGoods",description = "优选商品")
	public PickGoodsWithBLOBs outsourcingGoods(@Source EstateProjectActivityGoods source) {
		return pickGoodsService.getPickGoodsById(source.getPickGoodsId());
	}
	
	@TypeField(type = EstateProjectActivity.class, field = "image", description = "地产项目展示图")
    public StorageInfo image(@Source EstateProjectActivity source) {
        return storageService.get(source.getImage());
    }
	
	@TypeField(type = EstateProjectActivityHouseModel.class, field = "coverImage", description = "地产项目户型方案封面图")
    public StorageInfo coverImage(@Source EstateProjectActivityHouseModel source) {
        return storageService.get(source.getCoverImage());
    }
	
	@TypeField(type = EstateProjectActivityHouseModelItems.class, field = "jumpGoodsImage", description = "地产项目户型方案条目跳转图片")
    public StorageInfo itemsJumpGoodsImage(@Source EstateProjectActivityHouseModelItems source) {
		return storageService.get(source.getJumpGoodsImage());
    }
	
	@TypeField(type = EstateProjectActivityHouseModelItems.class, field = "images", description = "地产项目户型方案条目图片")
	public List<StorageInfo> images(@Source EstateProjectActivityHouseModelItems source) {
		String images = source.getImages();
		if(Check.notNull(images)) {
			JSONArray jsonArray = JSONArray.parseArray(images);
			List<String> list = jsonArray.toJavaList(String.class);
			return list.stream().map(storageId -> storageService.get(storageId)).collect(Collectors.toList());
		}
		return null;
	}
	
	@TypeField(type = StorageInfo.class, field = "houseModeItemsGoods", description = "方案条目图片购物")
	public List<EstateProjectActivityHouseModelItemsGoods> itemsGoods(@Source StorageInfo source){
		return service.listByStorageId(source.getStorageId());
	}
	
	@TypeField(type = EstateProjectActivityHouseModelItemsGoods.class,field = "estateProjectActivityGoods",description = "活动商品")
	public EstateProjectActivityGoods estateProjectActivityGoods(@Source EstateProjectActivityHouseModelItemsGoods source) {
		return service.getEstateProjectActivityGoods(source.getActivityGoodsId());
	}
	
	@TypeField(type = EstateProjectActivityHouseModel.class, field = "itemsList", description = "地产项目活动户型方案条目列表")
	public List<EstateProjectActivityHouseModelItems> itemsList(@Source EstateProjectActivityHouseModel source){
		return service.itemsList(source.getModelId());
	}
	
	@TypeField(type = EstateProjectActivityHouseModelItems.class, field = "estateProjectActivityGoodsList", description = "地产项目活动商品列表")
	public List<EstateProjectActivityGoods> estateProjectActivityGoodsList(@Source EstateProjectActivityHouseModelItems source) {
		return service.listByItemIdOnline(source.getItemId());
	}
	
	@QueryField(field = "estateProjectActivityList",description = "地产项目活动列表")
	public Pagination<EstateProjectActivity> query(@NonNull Page page, ConsumerMemberAuthority user){
		user.assertLogin();
		return service.queryOnline(page);
	}
	
	@QueryField(field = "estateProjectActivity",description = "地产项目活动详情")
	public EstateProjectActivity get(@NonNull Integer projectId, ConsumerMemberAuthority user){
		user.assertLogin();
		return service.getOnline(projectId);
	}
	
	@QueryField(field = "estateProjectActivityHouseModel",description = "地产项目活动户型方案详情")
	public EstateProjectActivityHouseModel estateProjectActivityHouseModel(@NonNull Integer modelId) {
		return service.getEstateProjectActivityHouseModel(modelId);
	}
}
